package com.example.localalbum.ui

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.utils.ImageUtil
import com.example.localalbum.common.LocalImageHelper
import kotlinx.android.synthetic.main.rv_image_item.view.*

/**
 * Created by fionera on 16-8-22.
 * 图片集适配器
 *
 */
internal class ImageAdapter(private val context: Context,
                            private val pictures: List<LocalImageHelper.LocalFile>,
                            private val names: List<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    companion object {
        val ITEM_TYPE_ADD = 1000
        val ITEM_TYPE_PIC = 2000
    }

    private var onItemClickListener: OnItemClickListener? = null
    private var onDeleteListener: OnDeleteListener? = null
    private var onAddListener: OnAddListener? = null

    var neverShow = false

    fun setOnDeleteListener(onDeleteListener: OnDeleteListener) {
        this.onDeleteListener = onDeleteListener
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        this.onItemClickListener = onItemClickListener
    }

    fun setOnAddListener(onAddListener: OnAddListener) {
        this.onAddListener = onAddListener
    }

    interface OnDeleteListener {
        fun onDelete(pos: Int)
    }

    interface OnItemClickListener {
        fun onClick(pos: Int)
    }

    interface OnAddListener {
        fun onClick()
    }

    /**
     * 多布局的实现
     */
    override fun getItemViewType(position: Int): Int = if (position == pictures.size) ITEM_TYPE_ADD else ITEM_TYPE_PIC

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder = if (viewType == ITEM_TYPE_PIC)
        ImageViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_image_item, parent, false))
    else
        ImageAddViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_image_item, parent, false))

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (ITEM_TYPE_ADD == getItemViewType(position)) {
            holder.itemView.iv_image_preview.setImageResource(R.drawable.iv_add_pic)
            holder.itemView.setOnClickListener {
                onAddListener?.onClick()
            }
            if (pictures.size >= LocalImageHelper.getInstance().totalSize || neverShow) {
                holder.itemView.iv_image_preview.visibility = View.GONE
            } else {
                holder.itemView.iv_image_preview.visibility = View.VISIBLE
            }
        } else {
            ImageUtil.loadThumbnail(pictures[position].thumbnailUri, holder.itemView.iv_image_preview, R.drawable.iv_default)
            if (position < names.size) {
                holder.itemView.tv_image_name.text = names[position]
                holder.itemView.tv_image_name.visibility = View.VISIBLE
            } else {
                holder.itemView.tv_image_name.visibility = View.GONE
            }
            holder.itemView.setOnClickListener {
                onItemClickListener?.onClick(holder.adapterPosition)
            }
            holder.itemView.setOnLongClickListener {
                onDeleteListener?.onDelete(holder.adapterPosition)
                true
            }
        }
    }

    override fun getItemCount(): Int {
        return pictures.size + 1
    }

    inner class ImageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class ImageAddViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
